package cn.codergege.demo
import cn.codergege.demo.dev.DbInit
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.CommandLineRunner
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Profile
import org.springframework.core.env.Environment
import org.springframework.core.env.StandardEnvironment

@SpringBootApplication
class BootstrapDemoApplication{
    @Autowired
    lateinit var env: Environment
    // for init dev data
    // 最好是在 CommandLineRunner 中直接通过 applicationContext 获取
    @Autowired
    var dbInit: DbInit? = null

    @Bean
    @Profile("dev")
    fun init() = CommandLineRunner {
        println("Active profiles from env: ${env.activeProfiles[0]}")
        // Do it only the first time!
        // 如果是 dev 模式, 对数据库进行初始化
//        if(env.activeProfiles[0] == "dev") dbInit?.initData()
    }
}

fun main(args: Array<String>) {
    println("Demo 开始运行...")
    runApplication<BootstrapDemoApplication>(*args) {
        val env = StandardEnvironment()
//        env.setActiveProfiles("dev")
        setEnvironment(env)
    }
}
